<template>
	<view>
		<van-tabs v-model="activeName" color="#1989fa" title-active-color="#1989fa" swipeable>
			<van-tab :title="zywjName" name="zywj">
				<van-list v-model="loading" :finished="finished" @load="onLoad">
					<van-cell v-for="item in zywjList" :title="item.title" :value="convertDate(item.pushTime)"
                    @click="toFileDetail(item.id)" value-class="text-sm" />
				</van-list>
			</van-tab>
      <van-tab :title="mhjwdName" name="mhjwd">
        <van-list v-model="loading" :finished="finished" @load="onLoad">
          <van-cell v-for="item in mhjwdList" :title="item.wdTitle" :value="item.publishDate"
                    @click="toWdDetail(item.wdId)" value-class="text-sm" />
        </van-list>
      </van-tab>
      <van-tab :title="zhswdName" name="zhswd">
        <van-list v-model="loading" :finished="finished" @load="onLoad">
          <van-cell v-for="item in zhswdList" :title="item.wdTitle" :value="item.publishDate"
                    @click="toWdDetail(item.wdId)" value-class="text-sm" />
        </van-list>
      </van-tab>
		</van-tabs>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			  userInfo: {},
				activeName: 'zywj',
        zywjName: '局领导重要讲话',
        zywjList: [],
        mhjwdName: '民航局文电',
        mhjwdList: [],
        zhswdName: '综合司文电',
        zhswdList: [],
        loading: false,
        finished: true,
        pageNum: 1,
        pageSize: 5,
			}
		},

    created() {
      this.userInfo = this.cache.get('userInfo')
    },

    mounted() {
		  // 初始化数据
      this.getZywjList()
      this.getMhjwdList()
      this.getZhswdList()
    },

		methods: {
		  // 获取重要文件
      getZywjList() {
        let _this = this
        _this.$http({
          url: _this.$http.adornUrl(`/caac/oa/app/getImportantDocuments`),
          method: 'get',
          params: _this.$http.adornParams({
            'page': _this.pageNum,
            'rows': _this.pageSize,
            'systemKey': this.userInfo.systemKey,
            'systemFlag': this.userInfo.systemFlag,
          })
        }).then(({data}) => {
          if(data.code == 200){
            if(data.data != null) {
              _this.zywjList = data.data.data.rows
            }
          }
        })
      },
      // 获取民航局文电
      getMhjwdList() {
        let _this = this
        _this.$http({
          url: _this.$http.adornUrl(`/caac/oa/doc/wdcx/mhj/getMhjWdList`),
          method: 'get',
          params: _this.$http.adornParams({
            'systemKey': this.userInfo.systemKey,
            'systemFlag': this.userInfo.systemFlag,
            'page': _this.pageNum,
            'rows': _this.pageSize,
          })
        }).then(({data}) => {
          if(data.code == 200) {
            _this.mhjwdList = data.data.rows
          }
        })
      },
      // 获取综合司文电
      getZhswdList() {
        let _this = this
        _this.$http({
          url: _this.$http.adornUrl(`/caac/oa/doc/wdcx/mhj/getSjWdManageList`),
          method: 'get',
          params: _this.$http.adornParams({
            'systemKey': this.userInfo.systemKey,
            'systemFlag': this.userInfo.systemFlag,
            'page': _this.pageNum,
            'rows': _this.pageSize,
          })
        }).then(({data}) => {
          if(data.code == 200) {
            _this.zhswdList = data.data.rows
          }
        })
      },

		  onLoad() {

      },

      // 转换日期
      convertDate(publishDate) {
        if(publishDate != null && publishDate.length > 10) {
          publishDate = publishDate.substring(0, 10)
        }
        return publishDate
      },

      // 打开文电
      toWdDetail(wdId) {
        let _this = this
        _this.$http({
          url: _this.$http.adornUrl(`/caac/oa/doc/wdcx/glj/getFileList`),
          method: 'get',
          params: _this.$http.adornParams({
            'systemKey': _this.cache.get('userInfo').systemKey,
            'systemFlag': _this.cache.get('userInfo').systemFlag,
            'wdId': wdId,
          })
        }).then(({data}) => {
          console.log('toFileDetail:' + JSON.stringify(data))
          if(data.code == 200) {
            if(_this.util.isNotEmpty(data.data[0])) {
              let attachId = data.data[0].attachId
              let attachName = data.data[0].attachName
              let attachType = data.data[0].attachType
              if (attachId) {
                attachType = attachType.toLowerCase()
                attachName = this.util.trim(attachName.split('.')[0]) + '.' + attachName.split('.')[1].toLowerCase()
                let url = '/caac/platform/UploadChunkController/download?attachId=' + attachId +
                    '&systemKey=' + this.cache.get('userInfo').systemKey +
                    '&systemFlag=' + this.cache.get('userInfo').systemFlag
                let openUrl = this.$http.adornUrl(url)
                // 打开文电
                _this.lx.openFileDetail(attachName, attachType.toLowerCase(),openUrl)
              }
            } else {
              _this.$toast('没有文件可供查看')
            }
          } else {
            _this.$toast(data.message)
          }
        })
      },


      // 跳转文件详情
      toFileDetail(id) {
        let _this = this
        _this.$http({
          url: _this.$http.adornUrl(`/caac/oa/app/getImportantDocumentsDetails`),
          method: 'get',
          params: _this.$http.adornParams({
            'systemKey': _this.cache.get('userInfo').systemKey,
            'systemFlag': _this.cache.get('userInfo').systemFlag,
            'id': id,
          })
        }).then(({data}) => {
          if(data.code == 200) {
            let annexs = data.data.data.annexs
            if (annexs == null || annexs.length == 0) {
              uni.navigateTo({
                url: '/pages/caac/components/newsList/compo-detail?from=zywj',
                success: function (res) {
                  // 通过eventChannel向被打开页面传送数据
                  res.eventChannel.emit('acceptDataFromOpenerPage', data.data.data)
                }
              })
            }else{
              let attachId = annexs[0].attachId
              if (attachId) {
                // 获取文件后缀
                this.getFileExtension(attachId)
              }
            }
          } else {
            _this.$toast(data.message)
          }
        })
      },
      // 获取文件后缀
      getFileExtension(attachId) {
        let _this = this
        _this.$http({
          url: _this.$http.adornUrl(`/caac/oa/app/getFileByIds`),
          method: 'get',
          params: _this.$http.adornParams({
            'systemKey': _this.cache.get('userInfo').systemKey,
            'systemFlag': _this.cache.get('userInfo').systemFlag,
            'attachId': attachId,
          })
        }).then(({data}) => {
          console.log('getFileExtension:' + JSON.stringify(data))
          if(data.code == 200) {
            if(_this.util.isNotEmpty(data.data.data[0])) {
              let attachType = data.data.data[0].attachType
              let attachName = data.data.data[0].attachName

              attachType = attachType.toLowerCase()
              attachName = this.util.trim(attachName.split('.')[0]) + '.' + attachName.split('.')[1].toLowerCase()
              let url = '/caac/oa/app/download?attachId=' + attachId + `&attachName=` + attachName +
                  '&systemKey=' + this.cache.get('userInfo').systemKey +
                  '&systemFlag=' + this.cache.get('userInfo').systemFlag
              let openUrl = this.$http.adornUrl(url)
              // 打开文件详情
              _this.lx.openFileDetail(attachName, attachType.toLowerCase(),openUrl)
            } else {
              _this.$toast('没有文件可供查看')
            }
          } else {
            _this.$toast(data.message)
          }
        })
      },


		},
	}
</script>

<style>
</style>
